<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/31
 * Time: 9:42
 */
namespace app\index\controller; 

use mikkle\tp_wechat\support\Db;
use think\Controller;
use think\Exception;

class Timing extends Controller{
	
	
	
	
		
	// 发送邮件
	public function eml_kjno()
	{
		//$title ='no表有未结算的记录';	
		/*$date = Db::query("SELECT czid from (
								SELECT czid,qihao from kkty_touzhu_list_no GROUP BY czid,qihao 
				        )as a GROUP BY czid HAVING(COUNT(*)>1) ");

		if($date){			
			foreach($date as $v){
				$czid =$v['czid'].'、';
			}						
		    $tosend = Db::name('xt_kflink')->where('type', 5)->column('qq');
			//循环发送
			foreach($tosend as $v){
				 $destination=$v;							
				$message='有未结算的记录 id '.$czid."请尽快处理";
				echo  setEmail($title,$message,$destination);
            }	
		}*/
		
		$date = Db::query("SELECT czid,qihao,count(*)as n from kkty_touzhu_list_no GROUP BY czid,qihao");  
	 
	 foreach($date as $v){	 
		 
				 $czid =$v['czid'];
				 $qihao =$v['qihao'];
				 $n =$v['n'];
				 
				 $date = Db::query("SELECT * from kkty_kjjg_list where czid= $czid and qihao=$qihao and kjjg >0");  
				
				if($date){
					 $tosend = Db::name('xt_kflink')->where('type', 5)->column('qq');
					 $title ='no表有未结算的记录 期号 '.$qihao."  id ".$czid;
					foreach($tosend as $v){
					 $destination=$v;							
					$message='您好，有未结算的记录， id：'.$czid." 期号是: ".$qihao." 投注记录".$n."  条 ， 请尽快处理。";
					echo  setEmail($title,$message,$destination);
                  }	
				}
				
	 }
	
	 
	}
	
	
     //定时清除开奖结果数据
    public function clearKjjg(){


        $low = [51,9,7,5,62,63,72]; //七星彩,港彩⑥合,福彩3D,排列三,分分时时彩,极速PK10,极速快三
		$limit = strtotime(date('Y-m-d 23:59:59',strtotime('-3 month')));
        if( Db::name("kjjg_list")->where(['czid'=>['not in',$low],'jztime'=>['<',$limit]])->delete() ){
            echo '清理A开奖结果成功'.PHP_EOL;
        }else{
            echo '清理A开奖结果失败'.PHP_EOL;
        }

        $limit_b = strtotime(date('Y-m-d 23:59:59',strtotime('-1 month')));

        if( Db::name("kjjg_list")->where(['czid'=>['in',[62,63,72]],'jztime'=>['<',$limit_b]])->delete() ){
            echo '清理B开奖结果成功'.PHP_EOL;
        }else{
            echo '清理B开奖结果失败'.PHP_EOL;
        }

    }
    //定时清除投注结果数据
    public function clearTouzhu(){

        $limit =strtotime(date('Y-m-d 23:59:59',strtotime('-2 month')));
        if( Db::name("touzhu_list_ok")->where('tz_time','<',$limit)->delete() ){
            echo '清理投注信息成功'.PHP_EOL;
        }else{
            echo '清理投注信息失败'.PHP_EOL;
        }

    }
    //更新每天最新一期开奖时间
    public function updateFirst(){
        $data_json = file_get_contents("http://a.apiplus.net/newly.do?token=E0C5726B8E45A086&format=json&rows=1");
        $data = json_decode($data_json, true);
        if($data && isset($data['data'])){
            $list = $data['data'];
            //PK10最早开奖时间 09:07:00
            $bjpk10_new = array();
            foreach($list as $v){
                if( 'bjpk10'==$v['code'] ){
                    $bjpk10_new = $v;
                    break;
                }
            }
            if( $bjpk10_new ){
                $qh = $bjpk10_new['expect'];
                $new_qh = intval($qh) + 1;
                $bjpk10 = Db::name('kjjg_list')->where(['czid'=>4,'qihao'=>['>',$qh]])->select();

                $time = Db::name("time")->where(['czid'=>4,'jztime'=>['>',date("H:i:s",$bjpk10_new['opentimestamp'])]])->value("jztime");
                $jztime = strtotime(date('Y-m-d ').$time);

                if( empty($bjpk10) ){
                    Db::name('kjjg_list')->data(['czid' => 4, 'qihao' => $new_qh, 'jztime' => $jztime, 'kjjg' => '0', 'iftz' => 0, 'ps' => '官方彩无预设'])->insert();
                    echo '0：更新信息成功';
                }elseif( count($bjpk10) > 1 || intval($new_qh)!=intval($bjpk10[0]['qihao']) ){
                    Db::startTrans();
                    try{
                        if( !Db::name('kjjg_list')->where(['czid' => 4, 'qihao' => ['>',$qh]])->delete() ){
                            throw new Exception('清理数据失败');
                        }
                        if( !Db::name('kjjg_list')->data(['czid' => 4, 'qihao' => $new_qh, 'jztime' => $jztime, 'kjjg' => '0', 'iftz' => 0, 'ps' => '官方彩无预设'])->insert() ){
                             throw new Exception('新增数据失败');
                        }
                        Db::commit();
                        echo '1：更新信息成功';
                    }catch (Exception $e){
                        Db::rollback();
                        echo '更新信息失败:'.$e->getMessage();
                    }
                }else{
                    echo '暂无更新信息';
                }
            }
        }
    }
}